package org.itboys.mobile.service.mysql.web;

import org.itboys.commons.ErrorException;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.order.SundayOrderProductMapper;
import org.itboys.mobile.entity.mysql.order.SundayOrder;
import org.itboys.mobile.entity.mysql.order.SundayOrderProduct;

import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2016年11月25日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_订单_后台_service
 */
@Service
public class SundayOrderService extends BaseMySqlService<SundayOrder> {
	@Autowired
	private SundayOrderProductMapper orderProductMapper;




	/**
	 * 分页获取订单
	 * @param request
	 * @param orderStatus
	 * @return
	 */
	public PageResult<SundayOrder> selectOrder(HttpServletRequest request, Integer orderStatus){
		Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"orderNo","memberName","receiveName");
		if(orderStatus!=null){
			param.put("orderStatus",orderStatus);
		}
	return  new PageResult<SundayOrder>(super.select(param),super.count(param));
	}

	/**
	 * 查询订单详情
	 * @param orderId
	 * @return
	 */
	public SundayOrder findOne(Long orderId){
		SundayOrder order = super.find(orderId);
		return order;
	}
	//分页获取订单商品
	public PageResult<SundayOrderProduct> selectOrderProduct(HttpServletRequest request, long orderId){
		Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"");
		param.put("orderId",orderId);
		long count = Long.valueOf(orderProductMapper.count(param).toString());
		return  new PageResult<SundayOrderProduct>(orderProductMapper.select(param),count);
	}


	/**
	 * 配送订单
	 * @param orderId
	 * @param sendCompany
	 * @param sendCompanyCode
	 * @param sendCode
	 */
	public void sendOrder(Long orderId, String sendCompany, String sendCompanyCode,  String sendCode){
		SundayOrder order = super.find(orderId);
		if(order.getOrderStatus()== SundayCommonConstants.ORDER_STATUS_DPS){
			throw new ErrorException("系统错误，请刷新后重试");
		}
		order.setSendCompany(sendCompany);
		order.setSendCompanyCode(sendCompanyCode);
		order.setSendCode(sendCode);
		order.setOrderStatus(SundayCommonConstants.ORDER_STATUS_PSZ);
		super.update(order);
	}



}
